package hnlg.com.cn.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import hnlg.com.cn.service.UnitService;
import hnlg.com.cn.vo.Unit;
import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
@RequestMapping("/UnitControl")
public class UnitController {
   List<Unit> list=null;
   @Resource UnitService unitService;
   
   @RequestMapping("/addUnit")
   //若不加responsebody则被模板引擎解析为页面跳转
   @ResponseBody
   public void addUnit(Unit unit) throws Exception{
	   log.info("开始增加Unit");
	   boolean flag=unitService.addUnit(unit);
	   if(flag==true){
		   log.info("增加成功");
	   }else{
		   log.info("增加失败");
	   }
   }
	
   @GetMapping("/deleteUnit")
   @ResponseBody
   public void deleteUnit(@RequestParam("id")int id){
	   log.info("开始删除Unit");
	   try {
		unitService.deleteUnit(id);
		log.info("删除成功");
	} catch (Exception e) {
		log.info("删除失败");
		e.printStackTrace();
	 }
   }
   @RequestMapping("/updateUnit")
   @ResponseBody
   public void updateUnit(Unit unit){
	   log.info("更新unit");
	   try {
		unitService.updateUnit(unit);
		log.info("更新成功");
	} catch (Exception e) {
	    log.info("更新失败");
		e.printStackTrace();
	}
	     
   }
   
   @RequestMapping("/deleteBatch")
   @ResponseBody
   public void deleteBatch(@RequestParam("ck") String checkbox[]){
	   log.info("开始批量删除---");
	   try{
		   for(String obj:checkbox){
			   System.out.println("checkbox="+obj);
		   }
		   this.unitService.deleteBatch(checkbox);
		   log.info("批量删除成功");
	   }catch(Exception e){
		   log.info("批量删除失败");
		   e.printStackTrace();
	   }
	   
   }
   
   @GetMapping("/queryUnit")
   @ResponseBody
   public List<Unit> queryUnit(@RequestParam(required=false,defaultValue="")String name){
	   log.info("开始查询Unit");
	   try{
		   Unit unit=new Unit();
		   unit.setName(name);
		   list=unitService.queryUnit(unit);
	   }catch(Exception e){
		   log.info("查询失败");
		   e.printStackTrace();
	   }
	   
	   return list;
   }
   
   
}
